package by.blog.dao.dao;

import by.blog.dao.beans.User;
import by.blog.dao.dao.factory.MysqlDaoFactory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Бондарчик Антон  15.11.2014.
 */
public class DaoUser implements DaoInterface<User , String>{

    /**
     * This fields are used for creating SQL query to table.
     * There are name of table and column names of this table.
     */
    private String tableName = "user";
    private String columnName1 = "user_name";
    private String columnName2 = "user_pass";

    /**
     * This method insert (User) user into table of DB.
     * @param user
     * @return true if the operation was successful, if else  - return false.
     */
    public boolean createUnit(User user) {

        try {
            String create = "INSERT INTO " + tableName + " ( " +
                    columnName1 + ", " +
                    columnName2 +
                    " ) VALUES (?, ?)";

            PreparedStatement statement = MysqlDaoFactory.prepareStatement(create);

            statement.setString(1, user.getUser_name());
            statement.setString(2, user.getUser_pass());

            statement.executeUpdate();
            return true;
        }
        catch (SQLException e) {

            return false;
        }
    }

    public User readUnit(String userName) {

        try {
            String read = "SELECT * FROM " + tableName + " WHERE " + columnName1 + "=?";
            PreparedStatement statement = MysqlDaoFactory.prepareStatement(read);

            statement.setString(1, userName);

            ResultSet result = statement.executeQuery();

            User user = null;

            while ((result).next()) {
                user = load(result);
            }
            return user;
        }
        catch (SQLException e) {

            return null;
        }
    }

    /**
     * This method create object (User) user via ResultSet.
     * @param RS (ResultSet)
     * @return User, created from ResultSet, or null if the operation wasn't successful.
     * @throws SQLException
     */
    private static User load(ResultSet RS)  throws SQLException {
        User user = new User();
        try {
            user.setUser_name(RS.getString(1));
            user.setUser_pass(RS.getString(2));
        }
        catch (SQLException e) {
            return null;
        }
        return user;
    }

    /**
     * This method update row in DB
     * @param user
     * @return true if the operation was successful, if else  - return false.
     */

    public boolean updateUnit(User user) {
        try {
            String update = "UPDATE " + tableName + " SET "
                    + columnName2 + " =? WHERE "         //user_pass
                    + columnName1 + "=?";

            PreparedStatement statement = MysqlDaoFactory.prepareStatement(update);

            statement.setString(1, user.getUser_pass());
            statement.setString(2, user.getUser_name());

            statement.executeUpdate();

            return true;
        } catch (SQLException e) {

            return false;
        }
    }

    /**
     * This method delete category from DB by index.
     * @param USER_NAME
     * @return true if the operation was successful, if else  - return false.
     */
    public boolean deleteUnit(String USER_NAME) {
        try {
            String del = "DELETE FROM " + tableName + " WHERE " + columnName1 + "=?";
            PreparedStatement statement = MysqlDaoFactory.prepareStatement(del);

            statement.setString(1, USER_NAME);
            statement.executeUpdate();

            return true;
        }
        catch (SQLException e) {

            return false;
        }
    }

    /**
     * This method select all objects from table and returns them in an List
     * @return List with all objects from table
     */
    public List<User> readAllUnits() {
        try {
            String read = "SELECT * FROM " + tableName;

            PreparedStatement statement = MysqlDaoFactory.prepareStatement(read);
            ResultSet result = statement.executeQuery();

            return loadUnits(result);
        }
        catch (SQLException e) {

            return null;
        }
    }

    /**
     * This method used to make readAllUnits() easier.
     * @param RS
     * @return List with objects from ResultSet
     */
    private static List<User> loadUnits(ResultSet RS) {
        try {
            List<User> userList = new ArrayList<User>();
            User user = null;

            while ((RS).next()) {

                user = load(RS);
                userList.add(user);
            }
            return userList;
        }
        catch (SQLException e) {
            return null;
        }
    }
}
